package com.yuantong.xyerp.core.provider.service;

import com.yuantong.xyerp.core.api.dto.UserDto;
import com.yuantong.xyerp.core.api.model.Menu;
import com.yuantong.xyerp.core.api.model.User;
import com.yuantong.xyerp.core.api.model.UserDetail;
import com.yuantong.xyerp.core.api.service.IUserService;
import com.yuantong.xyerp.core.provider.dao.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Jianguo.Yin on 2021-07-09.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetail selectOneByUserDetail(UserDetail userDetailTemp) {
        UserDetail userDetail = userMapper.selectOneByUserDetail(userDetailTemp);
        // 将所有权限整理为权限树
        List<Menu> menus = userDetail.getMenus();
        List<Menu> menusTree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            if (menus.get(i).getPid() == null) {
                List<Menu> subMenus = new ArrayList<>();
                int j = 0;
                while (j < menus.size()) {
                    if (menus.get(j).getPid() != null && menus.get(i).getId().equals(menus.get(j).getPid()))
                        subMenus.add(menus.get(j));
                    j++;
                }
                menus.get(i).setChildMenus(subMenus);
                menusTree.add(menus.get(i));
            }
        }
        userDetail.setMenus(menusTree);
        return userDetail;
    }

    public void userDetailCacheDelete(){}

    @Override
    public int update(User user) {
        assert user != null;
        return userMapper.update(user);
    }

    @Override
    public List<UserDto> findByFuzzy(String roleId, String organizationId, String fuzzy, List<UserDto> userDtos) {
        return userMapper.findByFuzzy(roleId,organizationId,fuzzy,userDtos);
    }

    /**
     * 根据 userDto 的 organizationId 获得该部门下所有员工 id
     *
     */
    @Override
    public List<String> getIdByOrganizationId(UserDto userDto) {
        List<UserDto> userDtos = userMapper.findByOrganizationId(userDto);
        // List<String> 里面存的是 u.id
        return userDtos.stream().map(UserDto::getId).collect(Collectors.toList());
    }

    /**
     * 根据 userDto 的 organizationId 获得该部门下所有员工 loginName
     *
     */
    @Override
    public List<String> getLoginNameByOrganizationId(UserDto userDto) {
        List<UserDto> userDtos = userMapper.findByOrganizationId(userDto);
        // List<String> 里面存的是 u.loginName
        return userDtos.stream().map(UserDto::getLoginName).collect(Collectors.toList());
    }

    /**
     * 根据 userDto 的 roleId 获得该角色下所有员工 loginName
     *
     */
    @Override
    public List<String> getLoginNameByRoleId(UserDto userDto) {
        List<UserDto> userDtos = userMapper.findByRoleId(userDto);
        // List<String> 里面存的是 u.loginName
        return userDtos.stream().map(UserDto::getLoginName).collect(Collectors.toList());
    }

    @Override
    public int saveBatchByUserDtos(List<UserDto> list) {
        return userMapper.saveBatchByUserDtos(list);
    }


}
